package cn.piaoyi.web.action;

import java.util.Date;
import java.util.List;
import cn.piaoyi.bean.Category;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.service.CategoryService;
import cn.piaoyi.service.GoodsService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;
import cn.piaoyi.web.interceptor.UserSessionAware;
import cn.piaoyi.web.servlet.UserSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class GoodsList extends BaseAction implements UserSessionAware{
	private static final Log logger = LogFactory.getLog(GoodsList.class);
	
	private GoodsService goodsService;
	private CategoryService categoryService;

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	
	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	
	private UserSession userSession;
	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	
	private String code = "concert";
	private int city = -1;
	private String time = "";
	private String startTime = "";
	private String endTime = "";
	private String prices = "all";
	private String startPrice = "";
	private String endPrice = "";
	private String name = "";

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public int getCity() {
		return city;
	}

	public void setCity(int city) {
		this.city = city;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getPrices() {
		return prices;
	}

	public void setPrices(String prices) {
		this.prices = prices;
	}

	public String getStartPrice() {
		return startPrice;
	}

	public void setStartPrice(String startPrice) {
		this.startPrice = startPrice;
	}

	public String getEndPrice() {
		return endPrice;
	}

	public void setEndPrice(String endPrice) {
		this.endPrice = endPrice;
	}	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}	
	
	
	private Category cagetory;
	private List categoryList;
	private PageList pageList;
	private List concertList;
	private List showList;
	private List dramaList;
	

	public Category getCagetory() {
		return cagetory;
	}

	public void setCagetory(Category cagetory) {
		this.cagetory = cagetory;
	}

	public List getConcertList() {
		return concertList;
	}

	public void setConcertList(List concertList) {
		this.concertList = concertList;
	}

	public List getShowList() {
		return showList;
	}

	public void setShowList(List showList) {
		this.showList = showList;
	}

	public List getDramaList() {
		return dramaList;
	}

	public void setDramaList(List dramaList) {
		this.dramaList = dramaList;
	}

	public List getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List categoryList) {
		this.categoryList = categoryList;
	}

	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		this.categoryList = this.getCategoryService().findCategorys("orders", 0);
		this.cagetory = this.getCategoryService().findCategoryByCode(this.code);
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		
		int t = -1;
		if(this.time.equalsIgnoreCase("today")){
			t = 0;
			startTime = "";
			endTime = "";
		}
		if(this.time.equalsIgnoreCase("tomorrow")){
			t = 1;
			startTime = "";
			endTime = "";
		}
		if(this.time.equalsIgnoreCase("month")){
			startTime = Util.formatDate(new Date());
			endTime = Util.formatDate(Util.getTime(Util.getYear(),Util.getMonth()+1,Util.getDay(),0,0,0));
		}
		int sp = -1;
		int ep = -1;
		if(StringUtils.isNotBlank(this.prices)){
			if(this.prices.equalsIgnoreCase("all")){
				sp = -1;
				ep = -1;
			}else{
				String[] strs = StringUtils.split(this.prices, ",");
				if(!strs[0].equalsIgnoreCase("0")){
					sp = Integer.parseInt(strs[0]);
				}
				if(!strs[1].equalsIgnoreCase("0")){
					ep = Integer.parseInt(strs[1]);
				}
			}
			startPrice = "";
			endPrice = "";
		}
		if(StringUtils.isNotBlank(startPrice)){
			sp = Integer.parseInt(startPrice);
		}
		if(StringUtils.isNotBlank(endPrice)){
			ep = Integer.parseInt(endPrice);
		}
		this.setPageList(this.getGoodsService().findGoodses(this.city,this.code, this.name, 1, -1, t, startTime, endTime, sp, ep, "id", 1, pages));
		this.concertList = this.getGoodsService().findGoodses(userSession.getCityId(),Constant.CATEGORY_CONCERT, "", 1, -1, "id", 1, 0, 6);
		this.showList = this.getGoodsService().findGoodses(userSession.getCityId(),Constant.CATEGORY_SHOW, "", 1, -1, "id", 1, 0, 6);
		this.dramaList = this.getGoodsService().findGoodses(userSession.getCityId(),Constant.CATEGORY_DRAMA, "", 1, -1, "id", 1, 0, 6);
		if(this.time.equalsIgnoreCase("month")){
			startTime = "";
			endTime = "";
		}
		pages.setFileName("/goods_list.action?city=" + this.city + "&code=" + this.code + "&time=" + this.time + "&startTime=" + this.startTime + "&endTime=" + this.endTime + "&prices=" + this.prices + "&startPrice=" + this.startPrice + "&endPrice=" + this.endPrice);
		return SUCCESS;
	}



}
